cap log close 
log using "${logpath}spillovers.log", replace

/*******************************************************************************
spillovers.do

Analyzes spillovers from districts subject to litigation to those nearby
*******************************************************************************/

use "${cleandatapath}dist_data.dta", clear

foreach type in criminal civil {
	gen months_since_`type'=month_date-first_`type'_spill if first_`type'_spill!=0
	replace months_since_`type'=. if months_since_`type'<-24 | months_since_`type'>23
	bysort dist_code: egen max_since_`type'=max(months_since_`type')
	bysort dist_code: egen min_since_`type'=min(months_since_`type')
	replace months_since_`type'=. if max_since_`type'!=23 | min_since_`type'!=-24
	replace months_since_`type'=-50 if first_`type'_spill==0 & `type'_spill_drop==0
	gen months_since_`type'100=months_since_`type'+100
	gen post_together_`type'=months_since_`type'100
	replace post_together_`type'=100 if months_since_`type'100>100 & !missing(months_since_`type'100)
}
drop if dist_code==14 // Dropping Connecticut

gen post_together=.
foreach var in log_tot_pmt log_rides {
	replace post_together=post_together_civil
	reghdfe `var' ib99.post_together i.post_crim i.prior_auth, a(dist_code month_date) vce(cluster dist_code)
	estadd ysumm
	estimates save ${estpath}TWFE_civil_`var'_spill, replace
	replace post_together=post_together_criminal
	reghdfe `var' ib99.post_together i.prior_auth i.post_civ, a(dist_code month_date) vce(cluster dist_code)
	estadd ysumm
	estimates save ${estpath}TWFE_criminal_`var'_spill, replace
}

estimates clear
estimates use ${estpath}TWFE_civil_log_tot_pmt_spill
	eststo est1
	eststo est1,addscalars(Year_FE 1)
	eststo est1,addscalars(Dist_FE 1)
estimates use ${estpath}TWFE_civil_log_rides_spill
	eststo est2
	eststo est2,addscalars(Year_FE 1)
	eststo est2,addscalars(Dist_FE 1)
estimates use ${estpath}TWFE_criminal_log_tot_pmt_spill
	eststo est3
	eststo est3,addscalars(Year_FE 1)
	eststo est3,addscalars(Dist_FE 1)
estimates use ${estpath}TWFE_criminal_log_rides_spill
	eststo est4
	eststo est4,addscalars(Year_FE 1)
	eststo est4,addscalars(Dist_FE 1)

label var tot_pmt "\shortstack{Total Ride \\ Payments}"
label var log_tot_pmt "\shortstack{Total Ride \\ Payments (Log)}"
label var ambulance_id "\shortstack{Total \\ Rides}"
label var log_rides "\shortstack{Total \\ Rides (Log)}"
label var active_firms "\shortstack{Active \\ Firms}"
label var log_active_firms "\shortstack{Active \\ Firms (Log)}"
label define post 100 "Neighboring Enforcement", replace
label values post_together post

*Table A25
esttab using ${outpath}Table_A25.tex, ///
	replace label se frag booktabs keep(*100*) ///
	star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
	mgroups("Civil" "Criminal", pattern(1 0 1 0) ///
		prefix(\multicolumn{@span}{c}{) suffix(}) span ///
		erepeat(\cmidrule(lr){@span})) ///
		stats(Year_FE Dist_FE ymean N, ///
			label("Year-Month FE" "District FE" "Dep. Var. Mean" "Observations"))

log close